// 日期转时间戳
export function getTimestamp(time) {
    if (!time && typeof time !== 'number') return time
    const _time = new Date(time)
    return _time.getTime()/1000
}

export function timestampEmpty(time, leve = 0) {
    if (!time && typeof time !== 'number') return time
    const _time = new Date(time)
    _time.setMilliseconds(0)
    _time.setSeconds(0)
    _time.setMinutes(0)
    _time.setHours(0)
    return _time.getTime()
}
export function timestampFull(time, leve = 0) {
    if (!time && typeof time !== 'number') return time
    const _time = new Date(time)
    _time.setMilliseconds(0)
    _time.setSeconds(59)
    _time.setMinutes(59)
    _time.setHours(23)
    return _time.getTime()
}
function returnTime(oDate) {
    if (oDate) {
        return {
            year: oDate.getFullYear(),
            month: oDate.getMonth() + 1,
            day: oDate.getDate(),
            hour: oDate.getHours(),
            min: oDate.getMinutes(),
            sen: oDate.getSeconds(),
            timestamp: oDate.getTime()
        }
    }
}

// 以天为单位 正则未来时间 负则过去时间
export const setDate = num => {
    return Date.now() + num * 24 * 60 * 60 * 1000
}
// 判断周几
export const getweek = timestamp => {
    const date = new Date(timestamp * 1)
    var week
    if (date.getDay() == 0) week = '周日'
    if (date.getDay() == 1) week = '周一'
    if (date.getDay() == 2) week = '周二'
    if (date.getDay() == 3) week = '周三'
    if (date.getDay() == 4) week = '周四'
    if (date.getDay() == 5) week = '周五'
    if (date.getDay() == 6) week = '周六'
    return week
}

// 24 小时以内显示多少小时之前， 24 小时开外显示日期, 1小时内显示分钟
export const time24 = timestamp => {
    if (!timestamp) return '--'
    let oldDate = ''
    switch ((timestamp + '').length) {
        case 19: // yyyy-MM-dd hh:mm:ss 格式的字符串
            oldDate = returnTime(new Date(timestamp))
            break
        case 10: // 秒时间戳
            oldDate = returnTime(new Date(+timestamp * 1000))
            break
        case 13: // 毫秒时间戳
            oldDate = returnTime(new Date(+timestamp))
            break
    }
    const newDate = returnTime(new Date())
    if (newDate.timestamp - oldDate.timestamp >= 24 * 60 * 60 * 1000 * 3) {
        return `${oldDate.month}月${oldDate.day}日`
    } else if (newDate.timestamp - oldDate.timestamp < 0) {
        return '1分钟前'
    } else if (newDate.timestamp - oldDate.timestamp < 60 * 60 * 1000) {
        if (newDate.timestamp - oldDate.timestamp < 60 * 1000) {
            return '1分钟前'
        } else {
            return Math.ceil((newDate.timestamp - oldDate.timestamp) / 1000 / 60) + '分钟前'
        }
    } else if (newDate.timestamp - oldDate.timestamp > 24 * 60 * 60 * 1000 && newDate.timestamp - oldDate.timestamp < 24 * 60 * 60 * 1000 * 3) {
        return Math.round((newDate.timestamp - oldDate.timestamp) / 1000 / 60 / 60 / 24) + '天前'
    } else {
        return Math.ceil((newDate.timestamp - oldDate.timestamp) / 1000 / 60 / 60) + '小时前'
    }
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length)
}
// 后端返回的时间格式分为三种 1. 毫秒时间 2. 秒时间 3. 年月日 时分秒时间
export function formatDate(oldDate, fmt = 'yyyy-MM-dd hh:mm') {
    if (!(oldDate * 1)) return '暂无数据'
    let date = ''
    switch ((oldDate + '').length) {
        case 19: // yyyy-MM-dd hh:mm:ss 格式的字符串
            date = new Date(oldDate)
            break
        case 10: // 秒时间戳
            date = new Date(+oldDate * 1000)
            break
        case 13: // 毫秒时间戳
            date = new Date(+oldDate)
            break
        case 14: // 毫秒时间戳
            date = new Date(+oldDate)
            break
    }

    // 正则匹配/(y+)/ 字符串fmt--'yyyy-MM-dd hh:mm'，如果匹配到，就获取第一个匹配的文本，即yyyy,
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        // 将yyyy替换成年份，如1970
        // date.getFullYear()的类型是number,(date.getFullYear() + '') 加个空字符串将number转成字符串类型
        // substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
    }
    const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    }
    for (const k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            const str = o[k] + ''
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
        }
    }
    return fmt
}

export function timeChange(time) {
    var date = new Date(time)
    // 有三种方式获取
    return date.getTime()
}

export const getAllDate = (start, end, type = 'MM-dd') => {
    const dateArr = []
    // let startArr = start.split('-')
    // let endArr = end.split('-')
    // let db = new Date()s
    // db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])
    // let de = new Date()
    // de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])
    const unixDb = new Date(start).getTime()
    const unixDe = new Date(end).getTime()
    let stamp
    const oneDay = 24 * 60 * 60 * 1000
    for (stamp = unixDb; stamp <= unixDe;) {
        dateArr.push(formatDate(parseInt(stamp), type))
        stamp = stamp + oneDay
    }
    return dateArr
}
// 检查时间是否过24小时
export function checkDays(time) {
    const now = Date.now()
    const d = new Date(time * 1)
    const secDiff = (now - d) / 1000
    return secDiff <= 24 * 60 * 60
}

export const howLong = (mss, val) => {
    const days = parseInt(mss / (1000 * 60 * 60 * 24))
    const hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
    const minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60))
    const sec = parseInt((mss % (1000 * 60 * 60)) / 1000)
    const seconed = Math.floor((mss / 1000) % 60)
    if (val === 'liveFive') {
        if (days > 0) {
            return String(days).padStart(2,'0') + ':' + String(hours).padStart(2,'0') + ':' + String(minutes).padStart(2,'0') + ':' + String(seconed).padStart(2,'0')
        }
        if (hours > 0) {
            return String(hours).padStart(2,'0') + ':' + String(minutes).padStart(2,'0') + ':' + String(seconed).padStart(2,'0')
        }
        if(minutes > 0){
            return String(hours).padStart(2,'0')+ ':' + String(minutes).padStart(2,'0') + ':' + String(seconed).padStart(2,'0')
        }
        return String(hours).padStart(2,'0')+ ':' + String(minutes).padStart(2,'0') + ':' + String(seconed).padStart(2,'0')
    }
    if (val === 'liveFlow') {
        if (days > 0) {
            return days + '天' + (hours > 0 ? hours + '时' : '')
        }
        if (hours > 0) {
            return hours + '时' + (minutes > 0 ? minutes + '分' : '')
        }
        if (minutes > 0) {
            return minutes + '分'
        }
    }
    if (val === 4) {
        if (days > 0) {
            return days + '天' + hours + '小时' + minutes + '分' + seconed + '秒'
        }
        if (hours > 0) {
            return hours + '小时' + minutes + '分' + seconed + '秒'
        }
        return minutes + '分' + seconed + '秒'
    }
    if (val === 'userSell') {
        if (days > 0) {
            return days + '天'
        }
        if (hours > 0) {
            return hours + '小时'
        }
        if (minutes > 0) {
            return minutes + '分钟'
        }
    }
    if (val === 'liveScreen') {
        if (days > 0) {
            return days + '天' + (hours > 0 ? hours + '小时' : '')
        }
        if (hours > 0) {
            return hours + '小时' + (minutes > 0 ? minutes + '分钟' : '')
        }
        if (minutes > 0) {
            return minutes + '分钟'
        }
    }
    if (val === 'liveDetail') {
        if (days > 0) {
            return days + '天' + hours + '小时'
        }
        if (hours > 0) {
            return hours + '小时' + minutes + '分钟'
        }
        if (minutes > 0) {
            return minutes + '分钟' + seconed + '秒'
        }
        if (seconed > 0) {
            return seconed + '秒'
        }
    }
    if (val === 'explain') {
        if (days > 0) {
            let result = days + '天'
            hours > 0 && (result += `${hours}小时`)
            return result
        }
        if (hours > 0) {
            let result = hours + '小时'
            minutes > 0 && (result += `${minutes}分钟`)
            return result
        }
        if (minutes > 0) {
            let result = minutes + '分钟'
            seconed > 0 && (result += `${seconed}秒`)
            return result
        }
        if (seconed > 0) {
            return seconed + '秒'
        }
    }
    if (days > 0) {
        return days + '天' + hours + '小时' + minutes + '分钟'
    }
    if (hours > 0) {
        return hours + '小时' + minutes + '分钟'
    }
    if (minutes > 0) {
        return minutes + '分钟'
    }
    if (sec > 0) {
        return sec + '秒'
    }
    return 0
}
export const stringToDate = (format) => {
    var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/
    var formatedDate = format.replace(pattern, '$1-$2-$3 $4:$5')
    return formatedDate
}
